<template>
  <editor-layout class="edit" @ok="handleOk" v-on="$listeners" v-loading="loading" :loading="saveLoading">
    <el-form class="full-form" size="small" :model="model" :rules="rule" ref="form" label-width="90px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="标题：">
            <el-input type="text" v-model="model.title" placeholder="标题" clearcbale/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类别标识：">
            <el-input type="text" v-model="model.type" placeholder="英文标识" clearcbale/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="类别中文：">
            <el-input type="text" v-model="model.typeCn" placeholder="中文备注" clearcbale/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="简介：">
            <el-input type="textarea" v-model="model.introduce" placeholder="简介" clearcbale/>
          </el-form-item>
        </el-col>
      </el-row>
      <my-editor v-if="false" ref="editor" class="full-editor" v-model="model.content" toolbar="classic"></my-editor>
      <div class="full-editor">
        <wang-editor v-model="model.content" />
      </div>
    </el-form>
  </editor-layout>
</template>

<script>

/**
 * Created by Administrator on 2021/7/30
 */
import {postSysContentSave, postSysContentUpdate, getSysContentInfoId} from '$my/code/api/content'

import wangEditor from '@/components/common/wang-editor'

export default {
  name: 'edit',
  components: {
    wangEditor
  },
  props: {
    id: String
  },
  data() {
    return {
      toolbar: [
        'heading', '|',
        'fontfamily', 'fontsize', '|',
        'alignment', '|',
        'fontColor', 'fontBackgroundColor', '|',
        'bold', 'italic', 'strikethrough', 'underline', 'subscript', 'superscript', '|',
        'link', '|',
        'outdent', 'indent', '|',
        'bulletedList', 'numberedList', 'todoList', '|',
        'code', 'codeBlock', '|',
        'insertTable', '|',
        'uploadImage', 'blockQuote', '|',
        'undo', 'redo'
      ],
      loading: false,
      saveLoading: false,
      model: {
        title: '',
        introduce: '',
        type: '',
        typeCn: '',
        content: ''
      },
      rule: {}
    }
  },
  methods: {
    async init() {
      if (this.id) {
        this.loadForm()
      }
    },
    async loadForm() {
      this.loading = true
      const data = await getSysContentInfoId(this.id).catch(e => {
        this.loading = false
        throw new Error(e)
      })
      const {id, title, type, typeCn, content, introduce} = data
      this.model = {id, title, type, typeCn, content, introduce}

      this.loading = false
    },
    handleOk() {
      let saveFn = postSysContentSave
      if(this.model.id) {
        saveFn = postSysContentUpdate
      }
      this.saveLoading = true

      saveFn({...this.model}).then(res => {
        this.saveLoading = false
        this.$notify.success('保存成功！')
        this.$emit('on-ok')

      }).catch(e => {
        console.info(e)
        this.saveLoading = false
      })
    }
  },

  mounted() {
    this.init()
  }
}
</script>

<style lang="scss" scoped>
.edit {

  .full-form {
    height: 100%;
    padding-right: 10px;
  }
  .full-editor {
    height: 80%;
    /deep/ {

      .w-e-text-container{
        height: calc(100% - 90px) !important;
      }
    }
  }
}
</style>
